package org.colin.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.dictPojo.DLanguage;
import org.colin.dictPojo.DStudyLevel;
import org.colin.dictPojo.DUserCollection;
import org.colin.dictPojo.SysAppUser;
import org.colin.model.ro.EmailRO;
import org.colin.service.*;
import org.colin.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RequestMapping("/app")
@Api(tags = "APP接口")
@RestController
public class AppUserController {


    @Autowired
    MailService mailService;
    @Autowired
    SysAppUserService sysAppUserService;

    @Autowired
    DStudyLevelService dStudyLevelService;

    @Autowired
    DEntryService dEntryService;
    @Autowired
    DLanguageService dLanguageService;

    @Autowired
    DUserCollectionService dUserCollectionService;
    @Autowired
    DUserEntryService dUserEntryService;


    @PostMapping("/sendrRegisterSms")
    @ApiOperation(value = "用户注册发送验证码")
    @LogAnnotation(title = "APP接口", action = "注册发送短信验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username" ,value = "用户名", paramType = "query",required = true),
    })
    public DataResult sendrRegisterSms(HttpServletRequest request , String username ) {
        DataResult success = DataResult.success();
        try {
            if(username == null || username.equals("")){
                return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败，用户名不能为空");
            }
            Integer verCode = (int)(Math.random()*9+1)*100000;
            String emailPattern = "^\\\\s*\\\\w+(?:\\\\.{0,1}[\\\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\\\.[a-zA-Z]+\\\\s*$";
            String phonePattern = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\\\d{8}$";
            if(Pattern.matches(emailPattern, username)){
                EmailRO emailRO = new EmailRO();
                emailRO.setTo(username);
                emailRO.setContent("验证码" + verCode);
                emailRO.setObject("验证码邮件");
                mailService.sendSimpleMail(emailRO);
            }else if(Pattern.matches(phonePattern, username)){
                //发短信
                SendSmsResponse response = SmsUtil.sendrRegisterSms(username , verCode.toString());
                System.out.println("短信接口返回的数据----------------");
                System.out.println("Code=" + response.getCode());
                System.out.println("Message=" + response.getMessage());
                System.out.println("RequestId=" + response.getRequestId());
                System.out.println("BizId=" + response.getBizId());
            }
            success.setData(verCode);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败,服务异常");
        }
        return success;
    }



    @PostMapping("/sendrRegisterCodeByUpdate")
    @ApiOperation(value = "用户修改手机号或邮箱发送验证码")
    @LogAnnotation(title = "APP接口", action = "用户修改手机号或邮箱发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username" ,value = "用户名", paramType = "query",required = true),
    })
    public DataResult sendrRegisterCodeByUpdate(HttpServletRequest request , String username ) {
        DataResult success = DataResult.success();
        try {
            if(username == null || username.equals("")){
                return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败，用户名不能为空");
            }
            Integer verCode = (int)(Math.random()*9+1)*100000;
            String emailPattern = "^\\\\s*\\\\w+(?:\\\\.{0,1}[\\\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\\\.[a-zA-Z]+\\\\s*$";
            String phonePattern = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\\\d{8}$";
            if(Pattern.matches(emailPattern, username)){
                EmailRO emailRO = new EmailRO();
                emailRO.setTo(username);
                emailRO.setContent("验证码" + verCode);
                emailRO.setObject("验证码邮件");
                mailService.sendSimpleMail(emailRO);
            }else if(Pattern.matches(phonePattern, username)){
                //发短信
                SendSmsResponse response = SmsUtil.sendrRegisterSms(username , verCode.toString());
                System.out.println("短信接口返回的数据----------------");
                System.out.println("Code=" + response.getCode());
                System.out.println("Message=" + response.getMessage());
                System.out.println("RequestId=" + response.getRequestId());
                System.out.println("BizId=" + response.getBizId());
            }
            success.setData(verCode);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败,服务异常");
        }
        return success;
    }





    @PostMapping("/registerUser")
    @ApiOperation(value = "注册用户")
    @LogAnnotation(title = "APP接口", action = "注册用户")
    public DataResult registerUser(HttpServletRequest request ,  @ApiParam(name="注册用户信息",value="用户信息",required=true) SysAppUser sysUser ) {
        if(sysUser.getUsername() == null || sysUser.getUsername().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "注册失败，用户名不能为空");
        }
        QueryWrapper<SysAppUser> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.eq("username",sysUser.getUsername());
        int count = sysAppUserService.count(sysAppUserQueryWrapper);
        if(count > 0 ){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"用户已被注册！");
        }

        //密码加密
        sysUser.setSalt(PasswordUtils.getSalt());
        String encode = PasswordUtils.encode(sysUser.getPassword(), sysUser.getSalt());
        sysUser.setPassword(encode);
        //主键 (根据当前时间创建ID，进行同步处理，同意时间只会生成一条，如果搭建集群，需要分布式事务)
        String userId = Tool.getPrimaryKey();
        sysUser.setId(userId);
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setStatus(1);
        sysUser.setDeleted(1);
        boolean save = sysAppUserService.save(sysUser);
        DataResult success = DataResult.success();
        success.setData(sysUser);
        if(save == true){
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"注册失败");
        }
    }




    @PostMapping("/loginUser")
    @ApiOperation(value = "用户登陆")
    @LogAnnotation(title = "APP接口", action = "用户登陆")
    public DataResult loginUser(HttpServletRequest request ,  @ApiParam(name="注册用户信息",value="用户信息",required=true) SysAppUser sysUser ) {
        if(sysUser.getUsername() == null || sysUser.getUsername().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "登陆失败，用户名不能为空");
        }
        if(sysUser.getPassword() == null || sysUser.getPassword().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "登陆失败，密码不能为空");
        }
        QueryWrapper<SysAppUser> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.eq("username",sysUser.getUsername());
        int count = sysAppUserService.count(sysAppUserQueryWrapper);
        if(count <= 0 ){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"用户未注册！");
        }
        SysAppUser one = sysAppUserService.getOne(sysAppUserQueryWrapper);
        //密码验证
        String encode = PasswordUtils.encode(sysUser.getPassword(), one.getSalt());
        if(!one.getPassword().equals(encode)){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"密码错误！");
        }
        one.setLastLoginDate(LocalDateTime.now());
        boolean update = sysAppUserService.updateById(one);
        DataResult success = DataResult.success();
        success.setData(sysUser);
        if(update == true){
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"登陆失败");
        }
    }




    @PostMapping("/retrievalUserByPhone")
    @ApiOperation(value = "找回密码-手机")
    @LogAnnotation(title = "APP接口", action = "找回密码-手机")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username" ,value = "用户名", paramType = "query",required = true),
    })
    public DataResult retrievalUserByPhone(HttpServletRequest request , String username ) {
        DataResult success = DataResult.success();
        try {
            if(username == null || username.equals("")){
                return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败，用户名不能为空");
            }
            Integer verCode = (int)(Math.random()*9+1)*100000;
            String phonePattern = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\\\d{8}$";
            if(Pattern.matches(phonePattern, username)){
                //发短信
                SendSmsResponse response = SmsUtil.sendRetrievalSms(username , verCode.toString());
                System.out.println("短信接口返回的数据----------------");
                System.out.println("Code=" + response.getCode());
                System.out.println("Message=" + response.getMessage());
                System.out.println("RequestId=" + response.getRequestId());
                System.out.println("BizId=" + response.getBizId());
            }else{
                return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败,手机格式有误");
            }
            success.setData(verCode);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败,服务异常");
        }
        return success;
    }



    @PostMapping("/retrievalUserByEmail")
    @ApiOperation(value = "找回密码-邮箱")
    @LogAnnotation(title = "APP接口", action = "找回密码-邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username" ,value = "用户名", paramType = "query",required = true),
    })
    public DataResult retrievalUserByEmail(HttpServletRequest request , String username ) {
        DataResult success = DataResult.success();
        try {
            if(username == null || username.equals("")){
                return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败，用户名不能为空");
            }
            Integer verCode = (int)(Math.random()*9+1)*100000;
            String emailPattern = "^\\\\s*\\\\w+(?:\\\\.{0,1}[\\\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\\\.[a-zA-Z]+\\\\s*$";
            if(Pattern.matches(emailPattern, username)){
                EmailRO emailRO = new EmailRO();
                emailRO.setTo(username);
                emailRO.setContent("验证码" + verCode);
                emailRO.setObject("验证码邮件");
                mailService.sendRetrievalSimpleMail(emailRO);
            }else{
                return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败,手机格式有误");
            }
            success.setData(verCode);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "发送失败,服务异常");
        }
        return success;
    }




    @PostMapping("/retrievalUser")
    @ApiOperation(value = "找回用户-修改密码")
    @LogAnnotation(title = "APP接口", action = "找回用户-修改密码")
    public DataResult retrievalUser(HttpServletRequest request ,  @ApiParam(name="注册用户信息",value="用户信息",required=true) SysAppUser sysUser ) {
        if(sysUser.getUsername() == null || sysUser.getUsername().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败，用户名不能为空");
        }
        SysAppUser sysAppUser = new SysAppUser();
        //密码加密
        sysAppUser.setSalt(PasswordUtils.getSalt());
        String encode = PasswordUtils.encode(sysUser.getPassword(), sysAppUser.getSalt());
        sysAppUser.setPassword(encode);
        QueryWrapper<SysAppUser> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.eq("username",sysUser.getUsername());
        boolean update = sysAppUserService.update(sysAppUser, sysAppUserQueryWrapper);
        DataResult success = DataResult.success();
        success.setData(sysUser);
        if(update == true){
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"找回失败");
        }
    }







    @PostMapping("/updateAppUser")
    @ApiOperation(value = "引导学习-修改语种和等级")
    @LogAnnotation(title = "APP接口", action = "引导学习-修改语种和等级")
    public DataResult updateAppUser(HttpServletRequest request ,  @ApiParam(name="修改信息",value="用户信息(信息选填，用户名必须)",required=true) SysAppUser sysUser ) {
        if(sysUser.getUsername() == null || sysUser.getUsername().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "注册失败，用户名不能为空");
        }

        SysAppUser sysAppUser = new SysAppUser();
        QueryWrapper<SysAppUser> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.eq("username",sysUser.getUsername());
        boolean update = sysAppUserService.update(sysAppUser, sysAppUserQueryWrapper);
        DataResult success = DataResult.success();
        success.setData(sysUser);
        if(update == true){
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"找回失败");
        }
    }


    @GetMapping("/languagelList")
    @ApiOperation(value = "获取语种列表")
    @LogAnnotation(title = "APP接口", action = "获取语种列表")
    public DataResult languagelList(HttpServletRequest request ) {
        List<DLanguage> list = dLanguageService.list();
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }


    @GetMapping("/studyLevelList")
    @ApiOperation(value = "获取学习等级列表")
    @LogAnnotation(title = "APP接口", action = "获取学习等级列表")
    public DataResult studyLevelList(HttpServletRequest request ) {
        List<DStudyLevel> list = dStudyLevelService.list();
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }


    @GetMapping("/indexSearchByPinYin")
    @ApiOperation(value = "索引查字-拼音")
    @LogAnnotation(title = "APP接口", action = "索引搜索-拼音")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "学习等级", paramType = "query",required = true),
    })
    public DataResult indexSearchByPinYin(HttpServletRequest request  , Integer language , Integer studyLevel) {
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        HashMap<String , Object> map = dEntryService.indexSearchByPinYin(language, studyLevel);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }



    @GetMapping("/indexSearchByPinYinDetail")
    @ApiOperation(value = "搜索--按拼音检索")
    @LogAnnotation(title = "APP接口", action = "搜索--按拼音检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "学习等级", paramType = "query",required = true),
            @ApiImplicitParam(name = "pinyin" ,value = "拼音", paramType = "query",required = true),
    })
    public DataResult indexSearchByPinYinDetail(HttpServletRequest request  , Integer language ,Integer studyLevel ,String pinyin) {
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        if(pinyin == null || pinyin.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，拼音不能为空");
        }
        Map<String , Object> map = dEntryService.indexSearchByPinYinDetail(language, studyLevel,pinyin);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }



    @GetMapping("/selectDentryById")
    @ApiOperation(value = "搜索--根据ID查详情")
    @LogAnnotation(title = "APP接口", action = "搜索--根据ID查详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "id" ,value = "词头ID", paramType = "query",required = true),
    })
    public DataResult selectDentryById(HttpServletRequest request  , Integer language ,Integer id) {
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，拼音id不能为空");
        }
        Map<String , Object> map = dEntryService.selectEntryById(id, language);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }



    @GetMapping("/indexSearchByStrokes")
    @ApiOperation(value = "索引查字--笔画")
    @LogAnnotation(title = "APP接口", action = "索引查字--笔画")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "hsk等级", paramType = "query",required = true),
    })
    public DataResult indexSearchByStrokes(HttpServletRequest request  , Integer studyLevel , Integer language) {
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        List map = dEntryService.indexSearchByStrokes( language  , studyLevel );
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }




    @GetMapping("/indexSearchByStrokesDetail")
    @ApiOperation(value = "搜索--按笔画检索")
    @LogAnnotation(title = "APP接口", action = "搜索--按笔画检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "hsk等级", paramType = "query",required = true),
            @ApiImplicitParam(name = "strokes" ,value = "笔画", paramType = "query",required = true),
    })
    public DataResult indexSearchByStrokesDetail(HttpServletRequest request  ,Integer studyLevel ,  Integer language , Integer strokes) {
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(strokes == null || strokes <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，笔画不能为空");
        }
        List map = dEntryService.indexSearchByStrokesDetail( language  , studyLevel ,strokes);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }


    @GetMapping("/indexSearchByParam")
    @ApiOperation(value = "搜索查字")
    @LogAnnotation(title = "APP接口", action = "搜索查字")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "hsk等级", paramType = "query",required = true),
            @ApiImplicitParam(name = "strokes" ,value = "笔画", paramType = "query",required = true),
    })
    public DataResult indexSearchByParam(HttpServletRequest request  ,Integer studyLevel ,Integer language ,String param) {
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(param == null || param.trim().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，搜索内容不能为空");
        }
        List map = dEntryService.indexSearchByParam( language  , studyLevel ,param);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }




    @GetMapping("/daySubject")
    @ApiOperation(value = "每日打卡")
    @LogAnnotation(title = "APP接口", action = "每日打卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "hsk等级", paramType = "query",required = true),
            @ApiImplicitParam(name = "num" ,value = "打卡数量", paramType = "query",required = true),
            @ApiImplicitParam(name = "userName" ,value = "答题人", paramType = "query",required = true),

    })
    public DataResult daySubject(HttpServletRequest request  ,Integer studyLevel ,  Integer language , Integer num ,String userName ) {
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(num == null || num <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，打卡数量不能为空");
        }
        if(userName == null || userName.trim().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，答题人不能为空");
        }
        List map = dEntryService.daySubject( language  , studyLevel ,num , userName);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }



    @GetMapping("/selectSubjectByEnterId")
    @ApiOperation(value = "每日打卡->根据词头查询所有题目")
    @LogAnnotation(title = "APP接口", action = "每日打卡->根据词头查询所有题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "词头ID", paramType = "query",required = true),
            @ApiImplicitParam(name = "language" ,value = "hsk等级", paramType = "query",required = true),
    })
    public DataResult selectSubjectByEnterId(HttpServletRequest request  ,Integer id ,  Integer language ) {
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(id == null || id <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，词头ID不能为空");
        }

        List<HashMap> map = dEntryService.selectSubjectByEnterId( id ,language);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }



    @PostMapping("/collectionEntry")
    @ApiOperation(value = "收藏词头")
    @LogAnnotation(title = "APP接口", action = "收藏词头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId" ,value = "词头ID", paramType = "query",required = true),
            @ApiImplicitParam(name = "userName" ,value = "收藏人", paramType = "query",required = true),
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
    })
    public DataResult collectionEntry(HttpServletRequest request , Integer dataId , String userName ,Integer language ) {
        if(dataId == null || dataId <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "收藏失败，词头ID不能为空");
        }
        if(userName == null || userName.trim().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "收藏失败，收藏人不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "收藏失败，收藏语种不能为空");
        }
        QueryWrapper<DUserCollection> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.eq("userName",userName);
        sysAppUserQueryWrapper.eq("type",1);
        sysAppUserQueryWrapper.eq("dataId",dataId);
        int count = dUserCollectionService.count(sysAppUserQueryWrapper);
        if(count > 0 ){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"该词头已被收藏！");
        }
        DUserCollection dUserCollection = new DUserCollection();
        dUserCollection.setDataId(dataId);
        dUserCollection.setType(1);
        dUserCollection.setCollectionDate(LocalDateTime.now());
        dUserCollection.setUserName(userName);
        boolean save = dUserCollectionService.save(dUserCollection);
        DataResult success = DataResult.success();
        if(save == true){
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"收藏失败");
        }
    }




    @PostMapping("/collectionSubject")
    @ApiOperation(value = "收藏题目")
    @LogAnnotation(title = "APP接口", action = "收藏题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId" ,value = "题目ID", paramType = "query",required = true),
            @ApiImplicitParam(name = "userName" ,value = "收藏人", paramType = "query",required = true),
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
    })
    public DataResult collectionSubject(HttpServletRequest request , Integer dataId , String userName , Integer language) {
        if(dataId == null || dataId <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "收藏失败，题目ID不能为空");
        }
        if(userName == null || userName.trim().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "收藏失败，收藏人不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "收藏失败，收藏语种不能为空");
        }
        QueryWrapper<DUserCollection> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.eq("type",2);
        sysAppUserQueryWrapper.eq("userName",userName);
        sysAppUserQueryWrapper.eq("dataId",dataId);
        int count = dUserCollectionService.count(sysAppUserQueryWrapper);
        if(count > 0 ){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"该题目已被收藏！");
        }
        DUserCollection dUserCollection = new DUserCollection();
        dUserCollection.setDataId(dataId);
        dUserCollection.setType(2);
        dUserCollection.setCollectionDate(LocalDateTime.now());
        dUserCollection.setUserName(userName);
        boolean save = dUserCollectionService.save(dUserCollection);
        DataResult success = DataResult.success();
        if(save == true){
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"收藏失败");
        }
    }


    @GetMapping("/entryStudy")
    @ApiOperation(value = "词汇学习")
    @LogAnnotation(title = "APP接口", action = "词汇学习")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "studyLevel" ,value = "hsk等级", paramType = "query",required = true),
            @ApiImplicitParam(name = "userName" ,value = "答题人", paramType = "query",required = true),

    })
    public DataResult entryStudy(HttpServletRequest request  ,Integer studyLevel ,  Integer language ,String userName ) {
        if(studyLevel == null || studyLevel <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，学习等级不能为空");
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(userName == null || userName.trim().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，答题人不能为空");
        }
        List map = dEntryService.entryStudy( language  , studyLevel  , userName);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }


    @GetMapping("/selectUserCollection")
    @ApiOperation(value = "收藏")
    @LogAnnotation(title = "APP接口", action = "收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query",required = true),
            @ApiImplicitParam(name = "userName" ,value = "答题人", paramType = "query",required = true),
            @ApiImplicitParam(name = "page" ,value = "当前页", paramType = "query",required = true),
            @ApiImplicitParam(name = "pageSize" ,value = "每页数量", paramType = "query",required = true),
    })
    public DataResult selectUserCollection(HttpServletRequest request   ,  Integer language ,String userName ,Integer  page ,Integer  pageSize ) {
        if(page == null || page <= 0){
            page = 1 ;
        }
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，语种不能为空");
        }
        if(userName == null || userName.trim().equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，答题人不能为空");
        }
        Page<HashMap> pageE = new Page<>(page, pageSize);
        List<HashMap> map = dEntryService.selectUserCollection(pageE ,  language   , userName);
        if(map.size() > 0) {
            DataResult success = DataResult.success();
            success.setData(map);
            return success;
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "搜索失败，未查到数据");
        }
    }







}
